#include "verilated.h"
// #include "verilated_vcd_c.h"
#include "verilated_fst_c.h"
#include "obj_dir/Vtwoxor.h"

VerilatedContext* contextp = NULL;
// VerilatedVcdC* tfp = NULL;
VerilatedFstC* tfp = NULL;
static Vtwoxor* top;

void step_and_dump_wave(){
  contextp->timeInc(1);
  top->eval();
}
void sim_init(){
  contextp = new VerilatedContext;
  // tfp = new VerilatedVcdC;
  tfp = new VerilatedFstC;
  top = new Vtwoxor;
  contextp->traceEverOn(true);
}

void sim_exit(){
  step_and_dump_wave();
}

int main(int argc, char** argv) {
  sim_init();
  contextp->commandArgs(argc, argv);
  Verilated::mkdir("logs");
  printf("%s/%s  \n", contextp->timeunitString(),contextp->timeprecisionString());
  top->trace(tfp, 0);
  tfp->set_time_unit("1ps");
  tfp->set_time_resolution("1ps");
  //printf("timeresstr: %d\n", tfp->spTrace()->timescaleToDouble(contextp->timeprecisionString()));
  // printf("timeresstr: %d\n", timescaleToDouble(contextp->timeprecisionString().c_str()));
  // tfp->open("dump.vcd");
  tfp->open("twoxor.fst");
/*  top->a=0; top->b=0;  step_and_dump_wave();   // 将s，a和b均初始化为“0”
                      top->b=1;  step_and_dump_wave();   // 将b改为“1”，s和a的值不变，继续保持“0”，
  top->b=0;  step_and_dump_wave();   // 将a，b分别改为“1”和“0”，s的值不变，
                      top->b=1;  step_and_dump_wave();   // 将b改为“1”，s和a的值不变，维持10个时间单位
  top->a=0; top->b=0;  step_and_dump_wave();   // 将s，a，b分别变为“1,0,0”，维持10个时间单位
                      top->b=1;  step_and_dump_wave();
            top->a=1; top->b=0;  step_and_dump_wave();
                      top->b=1;  step_and_dump_wave();
*/
  int i=0;
     while (!contextp->gotFinish()&&i<100) {
        contextp->timeInc(1);
        int a = rand() & 1;
        int b =  rand() & 1;
        top->a = a;
        top->b = b;
        top->eval();
        printf("a = %d, b = %d, f = %d\n", a, b, top->f);
        assert(top->f == (a^b));
	printf("%d\n", contextp->time());
        tfp->dump(contextp->time());
	i++;
    }
    tfp->close();
    delete contextp;
    delete top;
    return 0;
}
